{extend name="index/iframe" /}

{block name="content"}
<form class="layui-form layui-form-my" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">结账类型</label>
            <div class="layui-input-inline">
                <select name="balance_type" lay-filter="balance_type" id="balance_type" lay-verify="required">
                    <option value="">全部</option>
                    {foreach $Think.config.balance_type as $k=>$v}
                    <option value="{$k}">{$v}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">选择渠道</label>
            <div class="layui-input-inline">
                {MyTag:channel type='manage' /}
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">对账日期</label>
            <div class="layui-input-inline" id="audit_time">
                <input type="text" name="date" class="layui-input" lay-verify="required" id="date" placeholder="yyyy-MM"  lay-verify="date" autocomplete="off">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">发票类型</label>
            <div class="layui-input-inline">
                <select name="invoice_type" lay-filter="invoice_type" id="invoice_type" lay-verify="required">
                    <option value="">全部</option>
                    {volist name="$Think.config.cp_invoice_type" id="vo"}
                    <option value="{$vo.type}" rate="{$vo.rate}">{$vo.name}</option>
                    {/volist}
                </select>
            </div>
        </div>
    </div>
    <hr>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">SDK充值</label>
            <div class="layui-input-inline">
                <input type="text" value="0.00" name="sdk_fixed_price" id="sdk_fixed_price" placeholder="元" autocomplete="off" class="layui-input layui-disabled" disabled>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">通道费率</label>
            <div class="layui-input-inline">
                <input type="text" value="0" name="channel_rate" id="channel_rate" lay-verify="required" placeholder="%" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">税率</label>
            <div class="layui-input-inline">
                <input type="text" value="0" name="tax_rate" id="tax_rate" lay-verify="required" placeholder="%" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">分成比例</label>
            <div class="layui-input-inline">
                <input type="text" value="0" name="pay_rate" id="pay_rate" lay-verify="required" placeholder="%" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <!--<div class="layui-form-item">-->
        <!--<div class="layui-inline">-->
            <!--<label class="layui-form-label">分成金额</label>-->
            <!--<div class="layui-input-inline">-->
                <!--<input type="text" value="0.00" disabled id="all_fixed_price" lay-verify="required" placeholder="元" autocomplete="off" class="layui-input layui-disabled">-->
            <!--</div>-->
        <!--</div>-->
        <!--<div class="layui-inline">-->
            <!--<label class="layui-form-label">最终金额</label>-->
            <!--<div class="layui-input-inline">-->
                <!--<input type="text" value="0.00" name="all_total_price" id="all_total_price" lay-verify="required" placeholder="元" autocomplete="off" class="layui-input">-->
            <!--</div>-->
        <!--</div>-->
    <!--</div>-->
    <hr>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">乙方</label>
            <div class="layui-input-inline">
                <input type="text" value="" name="company" id="company" placeholder="渠道公司名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">开票内容</label>
            <div class="layui-input-inline">
                <select name="invoice_content" lay-filter="invoice_content" id="invoice_content">
                    <option value="">全部</option>
                    {foreach $Think.config.invoice_content as $k=>$v}
                    <option value="{$k}">{$v}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">注册地址</label>
            <div class="layui-input-inline">
                <input type="text" value="" name="reg_address" id="reg_address" placeholder="渠道公司注册地址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">注册电话</label>
            <div class="layui-input-inline">
                <input type="text" value="" name="contact_phone" id="contact_phone" placeholder="注册电话" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">游戏名称</label>
            <div class="layui-input-inline">
                <input type="text" value="多款" name="game_name" id="game_name" placeholder="游戏名称" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">开户名称</label>
            <div class="layui-input-inline">
                <input type="text" value="" name="open_name" id="open_name" placeholder="开户名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">开户银行</label>
            <div class="layui-input-inline">
                <input type="text" value="" name="open_bank" id="open_bank" placeholder="开户银行" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">开户账号</label>
            <div class="layui-input-inline">
                <input type="text" value="" name="open_account" id="open_account" placeholder="开户账号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">信用代码</label>
            <div class="layui-input-inline">
                <input type="text" value="" name="credit_code" id="credit_code" placeholder="统一社会信用代码" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-block">
                    <label class="layui-form-label">邮寄地址</label>
                    <div class="layui-input-block">
                        <input type="text" value="" name="send_address" id="send_address" placeholder="邮寄地址" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-md3">
                <div class="layui-block">
                    <label class="layui-form-label">联系人</label>
                    <div class="layui-input-block">
                        <input type="text" value="" name="contact_man" id="contact_man" placeholder="联系人" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-md3">
                <div class="layui-block">
                    <label class="layui-form-label">联系电话</label>
                    <div class="layui-input-block">
                        <input type="text" value="" name="contact_mobile" id="contact_mobile" placeholder="联系电话" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn my-btn" lay-submit="" lay-filter="submitBtn" >立即提交</button>
            <button class="layui-btn my-btn" lay-submit="" lay-filter="exportBtn" >导出</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
{/block}

{block name="css"}{/block}

{block name="js"}
<script>
    layui.use(['form', 'layedit', 'laydate', 'upload', 'element'], function(){
        var form = layui.form
            ,$ = layui.$
            ,layer = layui.layer
            ,laydate = layui.laydate;

        //选择游戏
        form.on('select(channel_id)', function(data){
            var channel_id = data.value
                ,date = $('input[name="date"]').val()
                ,invoice_type = $('#invoice_type').val()
                ,balance_type = $('#balance_type').val();

            if(channel_id && balance_type){
                time(channel_id, balance_type);
            }

            if(channel_id && date && invoice_type && balance_type){
                select(channel_id, date, invoice_type, balance_type);
            }
        });

        //发票类型
        form.on('select(invoice_type)', function(data){
            var channel_id = $('#channel_id').val()
                ,date = $('input[name="date"]').val()
                ,invoice_type = data.value
                ,balance_type = $('#balance_type').val();
            if(channel_id && date && invoice_type && balance_type){
                select(channel_id, date, invoice_type, balance_type);
            }
        });

        //结账类型
        form.on('select(balance_type)', function(data){
            var channel_id = $('#channel_id').val()
                ,date = $('input[name="date"]').val()
                ,invoice_type = $('#invoice_type').val()
                ,balance_type = data.value;

            if(channel_id && balance_type){
                time(channel_id, balance_type);
            }

            if(channel_id && date && invoice_type && balance_type){
                select(channel_id, date, invoice_type, balance_type);
            }
        });

        //获取对账时间
        function time(channel_id, balance_type){
            var load = layer.load();
            form.render();

            $.post('{:url("AuditSdkMng/time")}',{channel_id: channel_id, balance_type: balance_type}, function(json){
                layer.close(load);
                if(json.code==0){
                    var  d = new Date().getTime()
                        , html = '<input type="text" name="date" class="layui-input" lay-verify="required" id="date'+d+'" placeholder="yyyy-MM"  lay-verify="date" autocomplete="off">';
                    $('#audit_time').html(html);
                    console.dir(json.data);
                    laydate.render({
                        elem: '#date'+d
                        ,type: 'month'
                        ,min: json.data
                        ,max: '0'
                        ,done: function(value, date, endDate){
                            var channel_id = $('#channel_id').val()
                                ,date = value
                                ,invoice_type = $('#invoice_type').val()
                                ,balance_type = $('#balance_type').val();
                            if(channel_id && date && invoice_type && balance_type){
                                select(channel_id, date, invoice_type, balance_type);
                            }
                        }
                    });
                }else{
                    layer.msg(json.msg, {icon: 0, time: 1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        }

        //查询数据
        function select(channel_id, date, invoice_type, balance_type){
            var load = layer.load();
            $.post('{:url("AuditSdkMng/select")}',{channel_id: channel_id, date: date, invoice_type: invoice_type, balance_type: balance_type}, function(json){
                layer.close(load);
                if(json.code==0){
                    $('#sdk_fixed_price').val(json.data.sdk_fixed_price);
                    $('#channel_rate').val(json.data.channel_rate);
                    $('#tax_rate').val(json.data.tax_rate);
                    $('#pay_rate').val(json.data.pay_rate);
                    $('#all_fixed_price').val(json.data.all_total_price);
                    $('#all_total_price').val(json.data.all_total_price);
                    $('#company').val(json.data.company);
                    $('#reg_address').val(json.data.reg_address);
                    $('#contact_phone').val(json.data.contact_phone);
                    $('#contact_man').val(json.data.contact_man);
                    $('#open_name').val(json.data.open_name);
                    $('#open_bank').val(json.data.open_bank);
                    $('#open_account').val(json.data.open_account);
                    $('#credit_code').val(json.data.credit_code);
                    $('#send_address').val(json.data.send_address);
                    $('#contact_mobile').val(json.data.contact_mobile);
                }else{
                    layer.msg(json.msg, {icon: 0, time: 1000});
                    $('input[name="date"]').val('');
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        }
        $('#channel_rate').blur(function(){
            var sdk_fixed_price = $('#sdk_fixed_price').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,all_fixed_price = (sdk_fixed_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2);

            $('#all_fixed_price').val(all_fixed_price);
            $('#all_total_price').val(all_fixed_price);
        })
        $('#tax_rate').blur(function(){
            var sdk_fixed_price = $('#sdk_fixed_price').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,all_fixed_price = (sdk_fixed_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2);

            $('#all_fixed_price').val(all_fixed_price);
            $('#all_total_price').val(all_fixed_price);
        })
        $('#pay_rate').blur(function(){
            var sdk_fixed_price = $('#sdk_fixed_price').val()
                ,pay_rate = $('#pay_rate').val()
                ,channel_rate = $('#channel_rate').val()
                ,tax_rate = $('#tax_rate').val()
                ,all_fixed_price = (sdk_fixed_price*(pay_rate/100)*(1-(channel_rate/100))*(1-(tax_rate/100))).toFixed(2);

            $('#all_fixed_price').val(all_fixed_price);
            $('#all_total_price').val(all_fixed_price);
        })

        form.on('submit(submitBtn)', function(data){
            var load = layer.load();
            $.post('{:url("AuditSdkMng/add")}',data.field, function(json){
                layer.close(load);
                json.code==0 ? layer.msg(json.msg, {icon: 1, time: 1000}) : layer.msg(json.msg, {icon: 0, time: 1000});
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
            return false;
        });

        form.on('submit(exportBtn)', function(data){
            var str = '';
            layer.msg('生成中....', {icon: 1, time: 2000});
            for(key in data.field){
                str += key + '=' + data.field[key] + '&';
            }
            window.location.href = '{:url("AuditSdkMng/export2")}?'+str.substring(0, str.length-1);
            return false;
        });

    });
</script>
{/block}